package cas.ihep.hep.config.impl;

import cas.ihep.hep.config.Config;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class JSONConfig implements Config {
    private JSONObject mObj;

    private JSONConfig(JSONObject obj){
        mObj=obj;
    }

    public JSONConfig(String str){
        mObj=new JSONObject(str);
    }

    public JSONConfig(InputStream file){
        mObj=new JSONObject(new JSONTokener(file));
    }

    @Override
    public String getString(String key) throws ConfigException {
        try {
            return mObj.getString(key);
        }catch (JSONException ex){
            throw new ConfigException(ex);
        }
    }

    @Override
    public String getString(String key, String defVal) {
        return mObj.optString(key,defVal);
    }

    @Override
    public List<String> getStringArray(String key) throws ConfigException {
        try {
            JSONArray arr=mObj.getJSONArray(key);
            int len=arr.length();
            ArrayList<String> ret=new ArrayList<>(len);
            for(int i=0;i<len;i++){
                ret.add(arr.getString(i));
            }
            return ret;
        }catch (JSONException e){
            throw new ConfigException(e);
        }
    }

    @Override
    public int getInt(String key) throws ConfigException {
        try{
            return mObj.getInt(key);
        }catch (JSONException e){
            throw new ConfigException(e);
        }
    }

    @Override
    public int getInt(String key, int defVal) {
        return mObj.optInt(key,defVal);
    }

    @Override
    public List<Integer> getIntArray(String key) throws ConfigException {
        try{
            JSONArray arr=mObj.getJSONArray(key);
            int len=arr.length();
            ArrayList<Integer> ret=new ArrayList<>(len);
            for(int i=0;i<len;i++){
                ret.add(arr.getInt(i));
            }
            return ret;
        }catch(JSONException e){
            throw new ConfigException(e);
        }
    }

    @Override
    public long getLong(String key) throws ConfigException {
        try {
            return mObj.getLong(key);
        }catch(JSONException e){
            throw new ConfigException(e);
        }
    }

    @Override
    public long getLong(String key, long val) {
        return mObj.optLong(key,val);
    }

    @Override
    public List<Long> getLongArray(String key) throws ConfigException {
        try {
            JSONArray arr=mObj.getJSONArray(key);
            int len=arr.length();
            ArrayList<Long> ret=new ArrayList<>(len);
            for(int i=0;i<len;i++){
                ret.add(arr.getLong(i));
            }
            return ret;
        }catch (JSONException e){
            throw new ConfigException(e);
        }
    }

    @Override
    public float getFloat(String key) throws ConfigException {
        try {
            return mObj.getFloat(key);
        }catch (JSONException e){
            throw new ConfigException(e);
        }
    }

    @Override
    public float getFloat(String key, float val) {
        return mObj.optFloat(key,val);
    }

    @Override
    public List<Float> getFloatArray(String key) throws ConfigException {
        try {
            JSONArray arr=mObj.getJSONArray(key);
            int len=arr.length();
            ArrayList<Float> ret=new ArrayList<>(len);
            for(int i=0;i<len;i++){
                ret.add(arr.getFloat(i));
            }
            return ret;
        }catch(JSONException e){
            throw new ConfigException(e);
        }
    }

    @Override
    public double getDouble(String key) throws ConfigException {
        try {
            return mObj.getDouble(key);
        }catch (JSONException e){
            throw new ConfigException(e);
        }
    }

    @Override
    public double getDouble(String key, double val) {
        return mObj.optDouble(key,val);
    }

    @Override
    public List<Double> getDoubleArray(String key) throws ConfigException {
        try {
            JSONArray arr=mObj.getJSONArray(key);
            int len=arr.length();
            ArrayList<Double> ret=new ArrayList<>(len);
            for(int i=0;i<len;i++){
                ret.add(arr.getDouble(i));
            }
            return ret;
        }catch(JSONException e){
            throw new ConfigException(e);
        }
    }

    @Override
    public Config getConfig(String key) throws ConfigException {
        try {
            return new JSONConfig(mObj.getJSONObject(key));
        }catch (JSONException e){
            throw new ConfigException(e);
        }
    }

    @Override
    public List<Config> getConfigArray(String key) throws ConfigException {
        return null;
    }
}
